package com.FH.permission.orgnization_menu;

import com.FH.permission.privilege.PrivilegeDAO;
import com.FH.permission.sys_menu.SysmenuDAO;
import com.FH.permission.sys_menu.SysmenuShowDTO;
import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by acer on 2016/8/4.
 */
@Stateless
public class OrgnizationmenuDAOImpl extends AccessDB implements OrgnizationMenuDAO {

    @EJB
    private SysmenuDAO sysmenuDAO;
    @EJB
    private PrivilegeDAO privilegeDAO;


    @Override
    public ResultDTO getOrgnizationMenuInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from orgnization_menu where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("sys_menu_id")) {
                sql_w += " AND sys_menu_id = " + json.getString("sys_menu_id");
            }
            if (json.has("orgnization_menu_id")) {
                sql_w += " AND orgnization_menu_id = " + json.getString("orgnization_menu_id");
            }
            if (json.has("organization_id")) {
                sql_w += " AND organization_id = '" + json.getString("organization_id") + "'";

            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);//查询此数据是否存在

            List<String[]> seresult = new ArrayList<String[]>();

            List<OrgnizationMenuShowDTO> result = new ArrayList<OrgnizationMenuShowDTO>();
            List<SysmenuShowDTO> sysmenu = new ArrayList<SysmenuShowDTO>();

            if (!existName.equals("0")) {

                String sql = "select orgnization_menu_id,organization_id,sys_menu_id,orgnization_menu_create_time,orgnization_menu_desc, " +
                        "member_id from orgnization_menu where 1=1" + sql_w + " ORDER BY menu_order ASC " + sql_page;
                seresult = this.getReportingTo(sql);

                for (int i = 0; i < seresult.size(); i++) {
                    OrgnizationMenuShowDTO orgnizationMenuShowDTO = new OrgnizationMenuShowDTO();
                    orgnizationMenuShowDTO.setOrgnization_menu_id(seresult.get(i)[1]);
                    orgnizationMenuShowDTO.setOrganization_id(seresult.get(i)[2]);
                    orgnizationMenuShowDTO.setSys_menu_id(seresult.get(i)[3]);
                    orgnizationMenuShowDTO.setOrgnization_menu_create_time(seresult.get(i)[4]);
                    orgnizationMenuShowDTO.setOrgnization_menu_desc(seresult.get(i)[5]);
                    orgnizationMenuShowDTO.setMember_id(seresult.get(i)[6]);


                    String sys_menu_id = seresult.get(i)[3];//获取菜单id
                    ResultDTO sys_menu;
                    sys_menu = sysmenuDAO.getSysmenuInfo("{sys_menu_id:" + sys_menu_id + ",parent_sys_menu_id:\"\"}", "1", "10000");
                    orgnizationMenuShowDTO.setSysmenu(sys_menu.getValues());
                    List<SysmenuShowDTO> menu = sys_menu.getValues();
                    sysmenu.add(menu.get(0));

                    result.add(orgnizationMenuShowDTO);

                }
            }

            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setValues(sysmenu);
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO addOrgnizationMenuInfo(OrgnizationMenuPullDTO orgnizationMenuPullDTO) {
        String Sys_menu_ids[] = null;
        String menu_orders[] = null;
        int resultls = 1;//插入条数
        //查询结果
        ResultDTO resultDTO = new ResultDTO();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sql_w = "";//查询条件sql组合
        if (orgnizationMenuPullDTO.getSys_menu_id() != null) {
            String Sys_menu_id = orgnizationMenuPullDTO.getSys_menu_id();
            Sys_menu_ids = Sys_menu_id.split(",");
            menu_orders = orgnizationMenuPullDTO.getMenu_order().split(",");
        }
        if (orgnizationMenuPullDTO.getOrganization_id() != null) {
//            sql_w += " AND organization_id = '" + orgnizationMenuPullDTO.getOrganization_id() + "'";
            this.deleteOrgnizationMenuInfo(orgnizationMenuPullDTO);
        }
        for (int i = 0; i < Sys_menu_ids.length; i++) {
            String sys_menu_id = Sys_menu_ids[i];
            String menu_order = menu_orders[i];
//            String sql_k = " AND sys_menu_id = '" + sys_menu_id + "'";
//            sql_k = sql_k + sql_w;
//            String sql = "select count(1) from orgnization_menu where 1=1" + sql_k;
//            String sum = getReportingToOne(sql);
//            if (!sum.equals("1")) {
                String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
                String Privilegemenu_insert = "";
                Privilegemenu_insert = "insert into orgnization_menu(orgnization_menu_id,organization_id,sys_menu_id,orgnization_menu_create_time,orgnization_menu_desc," +
                        "member_id,menu_order) values ('" + uuid + "','" + orgnizationMenuPullDTO.getOrganization_id() + "','" + sys_menu_id + "'," +
                        "'" + df.format(new Date()) + "','" + orgnizationMenuPullDTO.getOrgnization_menu_desc() + "','" + orgnizationMenuPullDTO.getMember_id() + "','" + menu_order + "')";
                resultls = addReportingTo(Privilegemenu_insert);
                continue;
//            }
        }
        if (resultls != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion("菜单分配成功");
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion("菜单分配失败");
        }
        return resultDTO;
    }

    @Override
    public ResultDTO deleteOrgnizationMenuInfo(OrgnizationMenuPullDTO orgnizationMenuPullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        int result = 0;

        String sql = "select count(1) from orgnization_menu where organization_id = '" + orgnizationMenuPullDTO.getOrganization_id() + "'";

        String exist = getReportingToOne(sql);

        if (!exist.equals("0")) {

            String delete = "delete from orgnization_menu where organization_id = '" + orgnizationMenuPullDTO.getOrganization_id()+ "'";

            result = addReportingTo(delete);
        }
        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }

//    @Override
//    public ResultDTO updatePrivilegemenuInfo(PrivilegemenuPullDTO privilegemenuPullDTO) {
//        ResultDTO resultDTO=new ResultDTO();
//        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");
//
//        return null;
//    }
}

